package com.auxgroup.smarthome.inner.controller;

import com.auxgroup.bridge.business.inner.vo.DeviceExportDataVo;
import com.auxgroup.bridge.business.inner.vo.DeviceListVo;
import com.auxgroup.bridge.business.inner.vo.DeviceUserListVo;
import com.auxgroup.smarthome.inner.service.AppUserDBService;
import com.auxgroup.smarthome.inner.service.DeviceInfoDBService;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Author: laiqiuhua.
 * @Date: 2017/8/16 10:27.
 */
@Api(tags = "web设备管理")
@RestController
@RequestMapping("/products")
public class WebDeviceInfoController {

    @Autowired
    private DeviceInfoDBService deviceInfoDBService;
    @Autowired
    private AppUserDBService appUserDBService;

    @ApiOperation(value = "获取设备列表分页",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号Id",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "deviceStatus", value = "设备状态",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "keywords", value = "用mac/did/sn进行模糊查询",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码",  paramType = "query", dataType = "integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "分页大小",  paramType = "query", dataType = "integer", defaultValue = "20")
    })
    @GetMapping("/devices")
    public ApiResponse<PageContent<DeviceListVo>> getDeviceList(@RequestParam(value = "modelId", required = false) String modelId, @RequestParam(value = "keywords", required = false) String keywords, @RequestParam(value = "deviceStatus", required = false) String deviceStatus, @ApiIgnore Pageable pageable) {
        Page<DeviceListVo> page =  deviceInfoDBService.findDevicePage(modelId, deviceStatus, keywords, pageable);
        PageContent<DeviceListVo> pageContent = new PageContent(page);
        return ApiResponse.ok(pageContent);
    }

    @ApiOperation(value = "获取设备列表",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号Id",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "deviceStatus", value = "设备状态",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "keywords", value = "用mac/did/sn进行模糊查询",  paramType = "query", dataType = "string"),
    })
    @GetMapping("/devices/list")
    public ApiResponse<List<DeviceListVo>> getDeviceListByNoPage(String modelId, String keywords, String deviceStatus) {
        return ApiResponse.ok(deviceInfoDBService.findDeviceList(modelId, deviceStatus, keywords, null));
    }

    @ApiOperation(value = "获取导出设备列表",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号Id",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "deviceStatus", value = "设备状态",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "keywords", value = "用mac/did/sn进行模糊查询",  paramType = "query", dataType = "string"),
    })
    @GetMapping("/devices/export_data")
    public ApiResponse<List<DeviceExportDataVo>> getDeviceExportData(String modelId, String keywords, String deviceStatus) {
        return ApiResponse.ok(deviceInfoDBService.getDeviceExportData(modelId, deviceStatus, keywords));
    }


    @ApiOperation(value = "获取设备详情",notes = "")
    @GetMapping("/devices/{deviceId}")
    public ApiResponse<DeviceListVo> getDeviceDetail(@PathVariable("deviceId") String deviceId) {
        return deviceInfoDBService.findByDeviceId(deviceId);
    }


    @ApiOperation(value = "获取设备用户列表（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",  paramType = "query", dataType = "integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "分页大小",  paramType = "query", dataType = "integer", defaultValue = "20")
    })
    @GetMapping("/devices/{mac}/users")
    public ApiResponse<Page<DeviceUserListVo>> getDeviceUserList(@PathVariable("mac") String mac, Pageable pageable) {
        return appUserDBService.findDeviceUserPage(mac, pageable);
    }

    @ApiOperation(value = "获取设备故障列表")
    @GetMapping("/devices/{mac}/faults")
    public ApiResponse getDeviceFaultList(String mac, Pageable pageable) {
        return appUserDBService.findDeviceUserPage(mac, pageable);
    }


}
